package com.breeze.toolkit.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.breeze.toolkit.util.ConstantsUtil;
import com.breeze.toolkit.util.ResUtil;

import java.util.HashMap;

/**
 * Created by john on 2016/9/1.
 */
public abstract class BaseActivity extends FragmentActivity implements
		RefreshView.OnRefreshListener, UIUpdateListener {
	protected Context mContext;
	private int mBodyId;
	private BaseTitleView mBaseTitle;
	private RelativeLayout mBodyView;
	private HashMap<String, Fragment> mFragmentMap;
	private FragmentManager mFragmentManager;
	private RefreshView mRefreshView;
	private boolean mIsAdjust = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout rootView = (LinearLayout) LayoutInflater.from(this).inflate(
				ResUtil.getLayoutId(this, ConstantsUtil.ROOT_VIEW_LAYOUT_NAME),
				null, false);
		mBodyView = (RelativeLayout) rootView.findViewById(mBodyId = ResUtil.getViewId(this, ConstantsUtil.ROOT_VIEW_BODY_ID_NAME));
		ViewStub stub = (ViewStub) rootView.findViewById(ResUtil.getViewId(this, ConstantsUtil.TITLE_VIEW_ID_NAME));
		if (getViewTitle() != null && !getViewTitle().equals(0)) {
			mBaseTitle = (BaseTitleView) stub.inflate();

			mBaseTitle.setTitle(getViewTitle());

			initLeftBtn(mBaseTitle.getLeftBtn());
			mBaseTitle.getLeftBtn().setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onLeftClick(v);
				}
			});
			mBaseTitle.getLeftView().setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onLeftClick(v);
				}
			});

			initRightBtn(mBaseTitle.getRightBtn());
			mBaseTitle.getRightBtn().setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onRightClick(v);
				}
			});
		}

		View contentView;
		if (getLayoutId() > 0) {
			LayoutInflater.from(this).inflate(getLayoutId(), mBodyView, true);
		} else {
			contentView = getContentView();
			if (contentView != null) {
				mBodyView.addView(contentView);
			}
		}

		mRefreshView = new RefreshView(this);
		mRefreshView.setRefreshListener(this);
		mBodyView.addView(mRefreshView);

		setContentView(rootView);

		mContext = this;
		mFragmentManager = getSupportFragmentManager();
	}

	public RelativeLayout getBodyView() {
		return mBodyView;
	}

	public void setCustomTitle(String title) {
		if (mBaseTitle != null) {
			mBaseTitle.setTitle(title);
		}
	}

	public void add(Fragment fragment) {
		add(mBodyId, fragment);
	}

	public void setFragmentMap() {
		if (mFragmentMap == null) {
			this.mFragmentMap = new HashMap<>();
		}
	}

	public HashMap<String, Fragment> getFragmentMap() {
		return mFragmentMap;
	}

	public void addFragmentMapElement(String key, Fragment fragment) {
		if (mFragmentMap != null) {
			mFragmentMap.put(key, fragment);
		}
	}
	public boolean isNeedAddFragment(String name) {
		return mFragmentMap != null && mFragmentMap.get(name) == null;
	}

	public void add(int resId, Fragment fragment) {
		mFragmentManager.beginTransaction().add(resId, fragment).commitAllowingStateLoss();
		if (mFragmentMap != null) {
			mFragmentMap.put(fragment.getClass().getSimpleName(), fragment);
		}
	}

	public void show(Fragment fragment) {
		if (fragment != null) {
			mFragmentManager.beginTransaction().show(fragment).commitAllowingStateLoss();
		}
	}

	public void hide(Fragment fragment) {
		if (fragment != null) {
			mFragmentManager.beginTransaction().hide(fragment).commitAllowingStateLoss();
		}
	}

	public abstract int getLayoutId();

	public abstract Object getViewTitle();

	protected View getContentView() {
		return null;
	}

	public void initLeftBtn(Button left) {

	}

	public void initRightBtn(Button right) {

	}

	public void onLeftClick(View v) {
		onBackPressed();
	}

	public void onRightClick(View v) {

	}

	public void showRefreshView() {
		if (mRefreshView != null) {
			mRefreshView.setRefreshable(true);
			mRefreshView.updateVisibility(true);
		}
	}

	/**
	 * @param notice 无内容时展示文案
	 * @param solution 无内容时提示文案，可为null
	 */
	public void showContentEmptyView(String notice, String solution) {
		if (mRefreshView != null) {
			try {
				mRefreshView.setViewContent(ResUtil.getDrawableId(mContext, ConstantsUtil.TIP_EMPTY_IMAGE_NAME), notice, solution);
				mRefreshView.setRefreshable(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
			mRefreshView.updateVisibility(true);
		}
	}

	/**
	 * 调整刷新页面布局位置，某些页面特殊需求用
	 * @param topMargin 刷新页面顶部边距
	 */
	public void adjustRefreshViewTopMargin(int topMargin) {
		if (mRefreshView != null) {
			RelativeLayout.LayoutParams pa = (RelativeLayout.LayoutParams) mRefreshView.getLayoutParams();
			if (pa != null) {
				pa.topMargin = topMargin;
			}
		}
	}

	/**
	 * 调整刷新页面的布局位置
	 * @param header ListView的header部分, 若只部分展示无网刷新页面, 在ListView的addHeaderView后调用此方法即可
	 */
	public void adjustRefreshViewWithHeader(final View header) {
		if (header != null) {
			header.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					if (!mIsAdjust && header.getHeight() > 0) {
						mIsAdjust = true;
						adjustRefreshViewTopMargin(header.getHeight());
					}
				}
			});
		}
	}

	@Override
	public void refreshRequest() {

	}

	/**
	 * 网络连接异常时UI展示接口
	 *
	 * @param isConnected true 服务器连接失败, false 无网络
	 */
	@Override
	public void showConnectExceptionView(boolean isConnected) {
		if (isConnected) {
			mRefreshView.setViewContent(ResUtil.getDrawableId(mContext, ConstantsUtil.TIP_EMPTY_IMAGE_NAME),
			                            getString(ConstantsUtil.STRING_TIP_SERVER_ERROR_NAME),
			                            getString(ConstantsUtil.STRING_TIP_SOLUTION_NAME));
		}
		showRefreshView();
	}

	/**
	 * 服务端无数据返回时UI展示接口
	 */
	@Override
	public void showEmptyContentView() {
		showContentEmptyView(getString(ConstantsUtil.STRING_TIP_NO_DATA_NAME), null);
	}

	private String getString(String name) {
		return mContext.getResources().getString(ResUtil.getStringId(mContext, name));
	}
}
